
<?php 
echo "<pre>";
print_r($this->goodsList);
echo "</pre>";
?>

<div id="pageInfo" data-title="购物车"></div>
{if:$this->count == 0}
<!-- 无商品显示 -->
<section class="nodata">购物车中空空如也哦~</section>

{else:}

<!-- 优惠信息 -->
<section class="cart_prompt" style="display:none" id="cart_prompt">
	<h4>恭喜，您的订单已经满足了以下优惠活动！</h4>
	<ol></ol>
</section>

<!--促销模板-->
<script type="text/html" id="promotionTemplate">
	<li><%=item['plan']%>，<%=item['info']%></li>
</script>

<!-- 商品列表 -->
<section class="cart_list">
	<ul>
		{foreach:items = $this->goodsList key=$goodsKey}
		<script>var item{$goodsKey} = {echo:JSON::encode($item)};</script>
		<li>
			<div class="cart_list_goods">
				<div class="cart_list_photo" onclick="gourl('{url:/site/products/id/$item['goods_id']}')">
					<img src="{url:/pic/thumb/img/$item[img]/w/80/h/80}" alt="{$item['name']}">
				</div>
				<div class="cart_list_info">
					<h3 class="cart_list_info_title">{$item['name']}</h3>
					<p class="cart_list_info_info">
						{if:isset($item['spec_array'])} {set:$spec_array=Block::show_spec($item['spec_array']);}
						{foreach:items=$spec_array item=$specValue key=$specName}
						{$specName}：{$specValue}&nbsp;
						{/foreach}
						{else:}
						祝您购物愉快!
						{/if}
					</p>
					<em class="cart_list_info_price">单价：￥{$item['sell_price']}</em>
				</div>
			</div>
			<del class="del" onclick='javascript:removeCartByJSON(item{$goodsKey});'>删除</del>
			<div class="goods_num_adjust">
				<span onclick='cart_reduce(item{$goodsKey});'>-</span>
				<input type='text' onchange='cartCount(item{$goodsKey});' id="count_{$item['goods_id']}_{$item['product_id']}" value="{$item['count']}">
				<span onclick='cart_increase(item{$goodsKey});'>+</span>
			</div>
			<div class="count">小结：￥<span id="sum_{$item['goods_id']}_{$item['product_id']}">{$item['sum']}</span></div>
		</li>
		{/foreach}
	</ul>
</section>
<!-- 统计信息 -->
<section class="cart_count">
	<h4>购物车统计</h4>
	<table>
		<tr>
			<th>总重量</th>
			<th>总金额</th>
			<th>商品优惠</th>
			<th>促销优惠</th>
		</tr>
		<tr>
			<td><span id='weight'>{$this->weight}</span>g</td>
			<td>￥<span id='origin_price'>{$this->sum}</span></td>
			<td>￥<span id='discount_price'>{$this->reduce}</span></td>
			<td>￥<span id='promotion_price'>{$this->proReduce}</span></td>
		</tr>
	</table>
</section>

<footer class="cart_footer">
	<div class="cart_footer_fixed">
		<div class="buy btn_pay" id="btn_pay" onclick="gourl('{url:/simple/cart2}')">
			去结算
		</div>
		<div class="count">
			<span>合计:</span>
			<em>￥<i id='sum_price'>{$this->final_sum}</i></em>
			<u>不含运费</u>
		</div>
	</div>
</footer>
{/if}
<script>
$(function(){
	// 隐藏底部导航
	
	{if:$this->count != 0}
		hideNav();
	{/if}
	
	var car_li = $(".nav_cart");
	car_li.addClass("on");
	
	{if:$this->promotion}
	{foreach:items = $this->promotion}
	$('#cart_prompt ol').append( template.render('promotionTemplate',{"item":{echo:JSON::encode($item)}}) );
	{/foreach}
	$('#cart_prompt').show();
	{/if}

})
//购物车数量改动计算
function cartCount(obj)
{
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	var countInputVal = parseInt(countInput.val());
	var oldNum = countInput.data('oldNum') ? countInput.data('oldNum') : obj.count;

	//商品数量大于1件
	if(isNaN(countInputVal) || (countInputVal <= 0))
	{
		alert('购买的数量必须大于1件');
		countInput.val(1);
		countInput.change();
	}
	//商品数量小于库存量
	else if(countInputVal > parseInt(obj.store_nums))
	{
		alert('购买的数量不能大于此商品的库存量');
		countInput.val(parseInt(obj.store_nums));
		countInput.change();
	}
	else
	{
		var diff = parseInt(countInputVal) - parseInt(oldNum);
		if(diff == 0)
		{
			return;
		}

		var goods_id   = obj.product_id > 0 ? obj.product_id : obj.goods_id;
		var goods_type = obj.product_id > 0 ? "product"      : "goods";

		//更新购物车中此商品的数量
		$.getJSON("{url:/simple/joinCart}",{"goods_id":goods_id,"type":goods_type,"goods_num":diff,"random":Math.random()},function(content){
			if(content.isError == true)
			{
				alert(content.message);
				countInput.val(1);
				countInput.change();
			}
			else
			{
				var goodsId   = [];
				var productId = [];
				var num       = [];
				$('[id^="count_"]').each(function(i)
				{
					var idValue = $(this).attr('id');
					var dataArray = idValue.split("_");

					goodsId.push(dataArray[1]);
					productId.push(dataArray[2]);
					num.push(this.value);
				});
				countInput.data('oldNum',countInputVal);
				$.getJSON("{url:/simple/promotionRuleAjax}",{"goodsId":goodsId,"productId":productId,"num":num,"random":Math.random()},function(content){
					if(content.promotion.length > 0)
					{
						$('#cart_prompt li').remove();

						for(var i = 0;i < content.promotion.length; i++)
						{
							$('#cart_prompt ol').append( template.render('promotionTemplate',{"item":content.promotion[i]}) );
						}
						$('#cart_prompt').show();
					}
					else
					{
						$('#cart_prompt li').remove();
						$('#cart_prompt').hide();
					}

					/*开始更新数据*/
					$('#weight').html(content.weight);
					$('#origin_price').html(content.sum);
					$('#discount_price').html(content.reduce);
					$('#promotion_price').html(content.proReduce);
					$('#sum_price').html(content.final_sum);
					$('#sum_'+obj.goods_id+'_'+obj.product_id).html((obj.sell_price * countInputVal).toFixed(2));
				});
			}
		});
	}
}

//增加商品数量
function cart_increase(obj){
	//库存超量检查
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	if(parseInt(countInput.val()) + 1 > parseInt(obj.store_nums))
	{
		alert('购买的数量大于此商品的库存量');
	}
	else
	{
		countInput.val(parseInt(countInput.val()) + 1);
		countInput.change();
	}
}

//减少商品数量
function cart_reduce(obj){
	//库存超量检查
	var countInput = $('#count_'+obj.goods_id+'_'+obj.product_id);
	if(parseInt(countInput.val()) - 1 <= 0)
	{
		alert('购买的数量必须大于1件');
	}
	else
	{
		countInput.val(parseInt(countInput.val()) - 1);
		countInput.change();
	}
}

//移除购物车
function removeCartByJSON(obj)
{
	var goods_id   = obj.product_id > 0 ? obj.product_id : obj.goods_id;
	var goods_type = obj.product_id > 0 ? "product"      : "goods";
	$.getJSON("{url:/simple/removeCart}",{"goods_id":goods_id,"type":goods_type,"random":Math.random()},function()
	{
		window.location.reload();
	});
}
</script>